﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.SceneFramework.CSS {

	/// <summary>
	/// Style entity is a group of style value collections specified for each specified style state.
	/// </summary>
	public class StyleClass {
		private static int _styleStateCount;

		///<summary>Reference to owning style sheet</summary>
		private StyleSheet _styleSheet;	
		///<summary>Unique identifier(selector) of style entity in style sheet. </summary>
		private string _selector;
		///<summary>Collection of style value collections :)</summary>
		private StyleValueSet[] _styles;

		/// <summary>
		/// Gets the style state count.
		/// </summary>
		/// <value>The style state count.</value>
		public static int StyleStateCount {
			get {
				//of first access we need to figure out how many state we have
				if (_styleStateCount == 0) {
					_styleStateCount = Enum.GetValues(typeof(StyleState)).Length;
				}
				return _styleStateCount;
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="StyleClass"/> class.
		/// </summary>
		/// <param name="styleSheet">The owner of class - style sheet.</param>
		/// <param name="selector">Selector of this style entity. It is unique inside one stylesheet.</param>
		public StyleClass(StyleSheet styleSheet, String selector) {
			if (null == styleSheet) throw new ArgumentNullException("styleSheet");
			if (null == selector) throw new ArgumentNullException("selector");

			//initialize self
			_styleSheet = styleSheet;
			_selector = selector;
			_styles = new StyleValueSet[StyleStateCount];
		}

		#region Style Access

		/// <summary>
		/// Gets the <see cref="Vortex.SceneFramework.CSS.StyleValueSet"/> for the specified state. 
		/// </summary>
		/// <value><see cref="Vortex.SceneFramework.CSS.StyleValueSet"/> containing style values for specified style state;
		/// can be <c>null</c> if there are no values for specified state yet.</value>
		public StyleValueSet this[StyleState state] {
			get {
				return _styles[(int)state];
			}
		}

		/// <summary>
		/// Gets the style state for class creating empty one if it currently doesn't exist.
		/// </summary>
		/// <param name="state">The style state.</param>
		/// <returns>Instance of <see cref="StyleValueSet"/> </returns>
		public StyleValueSet GetOrCreateStateValueSet(StyleState state) {
			StyleValueSet result = _styles[(int)state];
			//check create new empty value set
			if (null == result) {
				_styles[(int)state] = result = new StyleValueSet();
			}
			return result;
		}

		#endregion

		/// <summary>
		/// Inherits the specified style entity copying all of properties from it.
		/// </summary>
		/// <param name="styleEntity">The parent entity to inherit.</param>
		public void Inherit(StyleClass styleEntity) {
			if (null == styleEntity) throw new ArgumentNullException("styleEntity");

			for (int n = 0; n < _styles.Length; ++n) {
				_styles[n].Copy(styleEntity._styles[n]);
			}
		}

		/// <summary>
		/// Merges current style entity with the specified one.
		/// </summary>
		/// <param name="styleEntity">The style entity to merge with.</param>
		public void Merge(StyleClass styleEntity) {
			if (null == styleEntity) throw new ArgumentNullException("styleEntity");

			for (int n = 0; n < _styles.Length; ++n) {
				_styles[n].Merge(styleEntity._styles[n]);
			}			
		}
	}
}
